package cn.meibang.common.beauty.provider.service;

import cn.apestar.core.conf.CommonConfig;
import cn.apestar.core.http.HeaderOSType;
import cn.apestar.core.json.JsonConverter;
import cn.apestar.core.utils.HttpUtils;
import cn.meibang.common.beauty.api.IProductAPIService;
import cn.meibang.common.beauty.provider.response.CardResponse;
import cn.meibang.common.beauty.provider.response.PackageResponse;
import cn.meibang.common.beauty.provider.response.ProductResponse;
import cn.meibang.common.beauty.provider.response.ServiceCategoryResponse;
import cn.meibang.common.beauty.provider.vo.ServiceCategoryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017-6-9.
 */
@Service
public class ProductAPIServiceProvider implements IProductAPIService {

    public static final String HEADER_SITE = "meibang";

    private String listServiceCategoryUrl = "php/item/php/category";
    private String listCardUrl = "php/item/php/CardList";
    private String listProductUrl = "php/item/php/goodsList";
    private String listPackageUrl = "php/item/php/packageList";

    @Autowired
    private CommonConfig commonConfig;

    @Autowired
    private JsonConverter jsonConverter;

    /**
     * 服务分类列表
     *
     * @return
     */
    @Override
    public List<ServiceCategoryVo> listServiceCategoryVo() {
        Map<String, String> headerMap = new HashMap();
        headerMap.put("site", HEADER_SITE);
        headerMap.put("os", HeaderOSType.WEB.getValue());

        String json = HttpUtils.httpGet(null, headerMap, commonConfig.getThirdUrl(), listServiceCategoryUrl);
        ServiceCategoryResponse serviceResponse = jsonConverter.toObject(json, ServiceCategoryResponse.class);
        return serviceResponse.getList();
    }

    /**
     * 卡项，即服务
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public CardResponse listCard(int page, int size) {
        return listCard(page, size, null);
    }

    /**
     * 产品列表
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public ProductResponse listProduct(int page, int size) {
        return listProduct(page, size, null);
    }

    /**
     * 疗程列表
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PackageResponse listPacakge(int page, int size) {
        return listPacakge(page, size, null);
    }

    @Override
    public CardResponse listCard(int page, int size, String itemName) {
        Map<String, String> headerMap = new HashMap();
        headerMap.put("site", HEADER_SITE);
        headerMap.put("os", HeaderOSType.WEB.getValue());

        Map<String, String> paramMap = new HashMap();
        paramMap.put("page", String.valueOf(page));
        paramMap.put("size", String.valueOf(size));
        if (StringUtils.isNotEmpty(itemName)) {
            paramMap.put("item_name", itemName);
        }

        String json = HttpUtils.httpGet(paramMap, headerMap, commonConfig.getThirdUrl(), listCardUrl);
        CardResponse res = jsonConverter.toObject(json, CardResponse.class);
        return res;
    }

    @Override
    public ProductResponse listProduct(int page, int size, String itemName) {
        Map<String, String> headerMap = new HashMap();
        headerMap.put("site", HEADER_SITE);
        headerMap.put("os", HeaderOSType.WEB.getValue());

        Map<String, String> paramMap = new HashMap();
        paramMap.put("page", String.valueOf(page));
        paramMap.put("size", String.valueOf(size));
        if (StringUtils.isNotEmpty(itemName)) {
            paramMap.put("item_name", itemName);
        }

        String json = HttpUtils.httpGet(paramMap, headerMap, commonConfig.getThirdUrl(), listProductUrl);
        ProductResponse res = jsonConverter.toObject(json, ProductResponse.class);
        return res;
    }

    @Override
    public PackageResponse listPacakge(int page, int size, String itemName) {
        Map<String, String> headerMap = new HashMap();
        headerMap.put("site", HEADER_SITE);
        headerMap.put("os", HeaderOSType.WEB.getValue());

        Map<String, String> paramMap = new HashMap();
        paramMap.put("page", String.valueOf(page));
        paramMap.put("size", String.valueOf(size));
        if (StringUtils.isNotEmpty(itemName)) {
            paramMap.put("item_name", itemName);
        }

        String json = HttpUtils.httpGet(paramMap, headerMap, commonConfig.getThirdUrl(), listPackageUrl);
        PackageResponse res = jsonConverter.toObject(json, PackageResponse.class);
        return res;
    }
}
